//
//  UIViewController+NavigationButton.m
//  Music-Baseline
//
//  Created by wangjun on 14-12-25.
//  Copyright (c) 2014年 Huawei. All rights reserved.
//

#import "UIViewController+NavigationButton.h"

static BOOL isButtonPressFlag = NO;

@implementation UIViewController (NavigationButton)


#pragma mark - Add Function Button
- (void)addCustumedBackButton
{
    UIButton *left = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 13.5, 25.5)];
    left.exclusiveTouch = YES;
    [left setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [left setImage:[UIImage imageNamed:@"back"] forState:UIControlStateHighlighted];
    if (self.navigationController.viewControllers.count > 1) {
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:left];
    }
    [left addTarget:self action:@selector(custumedBackButtonPressed) forControlEvents:UIControlEventTouchUpInside];
}


#pragma mark - Function Selector
- (void)custumedBackButtonPressed
{
    if (self.navigationController.viewControllers.count > 1)
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
    else
    {
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    }
}


- (BOOL)isAnotherButtonPress
{
    if (!isButtonPressFlag)
    {
        isButtonPressFlag = YES;
        double delayInSeconds = 0.3;
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            isButtonPressFlag = NO;
        });
        return NO;
    }
    else
    {
        return YES;
    }
}

@end
